package com.test.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.test.properties.AliPayProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author mpk
 * @since 2024/9/13 14:10:31
 */
@Configuration
@EnableConfigurationProperties(AliPayProperties.class)
public class AlibabaConfig {

    private final AliPayProperties aliPayProperties;

    public AlibabaConfig(AliPayProperties aliPayProperties) {
        this.aliPayProperties = aliPayProperties;
    }

    @Bean
    public AlipayClient alipayClient(){

        AlipayConfig aliPayConfig = new AlipayConfig();

        aliPayConfig.setServerUrl(aliPayProperties.getServerUrl());
        aliPayConfig.setAppId(aliPayProperties.getAppId());
        aliPayConfig.setPrivateKey(aliPayProperties.getAppPrivateKey());
        aliPayConfig.setAlipayPublicKey(aliPayProperties.getAliPayPublicKey());

        AlipayClient alipayClient;
        try {
            // 初始化支付宝客户端
            alipayClient = new DefaultAlipayClient(aliPayConfig);

        } catch (AlipayApiException e) {
            // 抛出运行时异常，通知调用者支付宝客户端初始化失败
            throw new RuntimeException(e);
        }

        return alipayClient;
    }



}
